﻿Imports System.Data
Imports System.Data.OleDb
Public Class BeachDetails
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Check if there is no Query String, if not redirects back to default page.
        If Request.Params("ID") = "" Then Response.Redirect("Default.aspx")

        If IsPostBack = False Then
            'Populate the tag cloud
            TagCloud.getTagCloud(SqlDataSource6, CloudTags, Page)

            'Populate the Comments
            Comments.GetComments(Request.Params("ID"), CommentsLtr)

            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
            oleDbConn.Open()

            'Populate the flag
            Dim SqlStringFlag As String = "SELECT BlueFlag FROM Beaches WHERE ID = @f1"
            Dim cmdFlag As OleDbCommand = New OleDbCommand(SqlStringFlag, oleDbConn)
            cmdFlag.CommandType = CommandType.Text
            cmdFlag.Parameters.AddWithValue("@f1", Request.Params("ID"))
            Dim drFlag As OleDbDataReader = cmdFlag.ExecuteReader()
            drFlag.Read()
            If (drFlag("BlueFlag") = -1) Then
                img_Flag.ImageUrl = "Images/blueFlag.jpg"
            Else
                img_Flag.Visible = False
            End If

            'Populate the repeaters for displaying similar rated beaches and beaches near this beach

            SqlDataSource10.SelectParameters("@AverageRating").DefaultValue = getAverageRating.ToString
            SqlDataSource11.SelectParameters("@Region").DefaultValue = getRegion()

            'Checking if user has selected this beach as favorite beach
            Dim SqlString As String = "SELECT * FROM Favorite_Beach WHERE Tourist = @f1 AND Beach_FK = @f2"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            cmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
            'Getting records (only 1 line for selected tourist)
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("Beach_FK")) Then
                    btn_Favorites.Text = "Your Favorite Beach"
                    btn_Favorites.Enabled = False
                End If
            End If

            'Check if user has add this beach to his guide
            Dim CheckGuideSqlString As String = "SELECT * FROM Guides WHERE GuideCreator = @f1 AND Beach_FK = @f2"
            Dim cmdGuide As OleDbCommand = New OleDbCommand(CheckGuideSqlString, oleDbConn)
            cmdGuide.CommandType = CommandType.Text
            cmdGuide.Parameters.AddWithValue("@f1", User.Identity.Name)
            cmdGuide.Parameters.AddWithValue("@f2", Request.Params("ID"))
            'Getting records (only 1 line for selected tourist)
            Dim drGuide As OleDbDataReader = cmdGuide.ExecuteReader()
            If drGuide.HasRows Then
                drGuide.Read()
                If Not IsDBNull(drGuide("Beach_FK")) Then
                    btn_AddGuide.Text = "Added to Guide"
                    btn_AddGuide.Enabled = False
                End If
            End If

            'Counting the activity that the user has viewed this particular beach
            Dim beachesViewedSql As String = "INSERT INTO Beaches_Viewed (Tourist, Beach_FK) VALUES (@f1, @f2)"
            Dim cmdViewed As OleDbCommand = New OleDbCommand(beachesViewedSql, oleDbConn)
            cmdViewed.CommandType = CommandType.Text
            cmdViewed.Parameters.AddWithValue("@f1", User.Identity.Name)
            cmdViewed.Parameters.AddWithValue("@f2", Request.Params("ID"))
            cmdViewed.ExecuteNonQuery()


            'Checking if user has rated this beach
            Dim checkRatingSql As String = "SELECT * FROM BeachUserRating WHERE Tourist = @f1 AND Beach_FK = @f2"
            Dim cmdCheckRating As OleDbCommand = New OleDbCommand(checkRatingSql, oleDbConn)

            cmdCheckRating.CommandType = CommandType.Text
            cmdCheckRating.Parameters.AddWithValue("@f1", User.Identity.Name)
            cmdCheckRating.Parameters.AddWithValue("@f2", Request.Params("ID"))
            'Getting records (only 1 line for selected tourist)
            Dim drRating As OleDbDataReader = cmdCheckRating.ExecuteReader()
            If drRating.HasRows Then
                drRating.Read()
                If Not IsDBNull(drRating("Beach_FK")) Then
                    btn_Rating.Visible = False
                    btn_Change.Visible = True
                    Repeater9.Visible = True
                    Rating1.Visible = False
                End If
            End If

            'Count Activity for page
            Dim SqlStringActivity As String = "INSERT INTO SiteActivity(Activity) VALUES (@f1)"
            Dim cmdActivity As OleDbCommand = New OleDbCommand(SqlStringActivity, oleDbConn)
            cmdActivity.CommandType = CommandType.Text
            cmdActivity.Parameters.AddWithValue("@f1", "Beaches")
            cmdActivity.ExecuteNonQuery()

            'Check if user likes this beach
            Dim doesLikesql As String = "SELECT * FROM Beach_Likes WHERE Beach_FK = @f1 AND Tourist = @f2"
            Dim doesLikecmd As OleDbCommand = New OleDbCommand(doesLikesql, oleDbConn)
            doesLikecmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            doesLikecmd.Parameters.AddWithValue("@f2", User.Identity.Name)

            'Find out if there any records at all (returned)
            Dim drLikes As OleDbDataReader = doesLikecmd.ExecuteReader()
            'this shows if any data is returned
            If drLikes.HasRows Then
                btn_Likes.Enabled = False
                btn_Likes.Text = "Liked"
            Else
                btn_Likes.Enabled = True
                btn_Likes.Text = "Like it"
            End If
            oleDbConn.Close()
            oleDbConn.Dispose()
        End If

    End Sub

    Protected Function getRating(ByVal rating As Integer) As String
        Select Case (rating)
            Case Is = 1
                Return "Images/Rating/1-umbrellaRed-rating.png"
            Case Is = 2
                Return "Images/Rating/2-umbrellaRed-rating.png"
            Case Is = 3
                Return "Images/Rating/3-umbrellaRed-rating.png"
            Case Is = 4
                Return "Images/Rating/4-umbrellaRed-rating.png"
            Case Is = 5
                Return "Images/Rating/5-umbrellaRed-rating.png"
        End Select
        Return Nothing
    End Function

    Protected Sub btn_Tag_Click(sender As Object, e As EventArgs) Handles btn_Tag.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "INSERT INTO BeachTagJunction (Beach_FK, Tag_FK, Tourist) VALUES (@f1, @f2, @f3)"
        oleDbConn.Open()
        For Each cd As ListItem In cbl_Tags.Items
            If cd.Selected Then
                'Check if user has added the same tag already
                Dim sqlCheckTag As String = "SELECT COUNT(*) FROM BeachTagJunction WHERE Beach_FK = @f4 AND Tourist = @f5 AND Tag_FK = @f6"
                Dim cmdCheck As OleDbCommand = New OleDbCommand(sqlCheckTag, oleDbConn)
                cmdCheck.CommandType = CommandType.Text
                cmdCheck.Parameters.AddWithValue("@f4", Request.Params("ID"))
                cmdCheck.Parameters.AddWithValue("@f5", User.Identity.Name)
                cmdCheck.Parameters.AddWithValue("@f6", cd.Value)
                Dim userCount As Integer = cmdCheck.ExecuteScalar()
                If (userCount > 0) Then
                    lbl_Tag.Text = "You have already added that tag"
                Else
                    'Add the join to the table
                    Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
                    cmd.CommandType = CommandType.Text
                    cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
                    cmd.Parameters.AddWithValue("@f2", cd.Value)
                    cmd.Parameters.AddWithValue("@f3", User.Identity.Name)
                    cmd.ExecuteNonQuery()
                    lbl_Tag.Text = ""
                End If
            End If
        Next
        'check if text box is empty
        If tb_Tags.Text = "" Then
            oleDbConn.Close()
            TagCloud.getTagCloud(SqlDataSource6, CloudTags, Page)
            cbl_Tags.ClearSelection()
            tb_Tags.Text = ""
        Else
            Dim newTags = tb_Tags.Text.Split(",")
            For Each newTag In newTags
                'Check if tag already exists

                Dim cdTemp As Integer
                cdTemp = -1
                For Each cd As ListItem In cbl_Tags.Items
                    If cd.Text.ToLower.Equals(newTag.ToLower) Then
                        cdTemp = cd.Value

                    End If
                Next
                If cdTemp = -1 Then
                    'Add new tag to the Tags table
                    Dim newTagSql = "INSERT INTO Tags(Tag) VALUES (@f1)"
                    Dim cmd As OleDbCommand = New OleDbCommand(newTagSql, oleDbConn)
                    cmd.CommandType = CommandType.Text
                    cmd.Parameters.AddWithValue("@f1", newTag)
                    cmd.ExecuteNonQuery()

                    'Find id of the new tag.
                    Dim getLastIdCmd As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID FROM Tags ORDER BY ID DESC", oleDbConn)
                    Dim dr = getLastIdCmd.ExecuteReader()
                    dr.Read()
                    Dim lastId = dr.GetValue(0)

                    'Assossiate this last id with the hotel id
                    Dim newCmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
                    newCmd.CommandType = CommandType.Text
                    newCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
                    newCmd.Parameters.AddWithValue("@f2", lastId)
                    newCmd.Parameters.AddWithValue("@f3", User.Identity.Name)
                    newCmd.ExecuteNonQuery()

                Else

                    'Check if user has added the same tag already
                    Dim sqlCheckTag2 As String = "SELECT COUNT(*) FROM BeachTagJunction WHERE Beach_FK = @f1 AND Tourist = @f2 AND Tag_FK = @f3"
                    Dim cmdCheck2 As OleDbCommand = New OleDbCommand(sqlCheckTag2, oleDbConn)
                    cmdCheck2.CommandType = CommandType.Text
                    cmdCheck2.Parameters.AddWithValue("@f1", Request.Params("ID"))
                    cmdCheck2.Parameters.AddWithValue("@f2", User.Identity.Name)
                    cmdCheck2.Parameters.AddWithValue("@f3", cdTemp)
                    Dim userCount2 As Integer = cmdCheck2.ExecuteScalar()
                    If (userCount2 = 0) Then

                        'Assossiate this last id with the hotel id
                        Dim newCmd1 As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
                        newCmd1.CommandType = CommandType.Text
                        newCmd1.Parameters.AddWithValue("@f1", Request.Params("ID"))
                        newCmd1.Parameters.AddWithValue("@f2", cdTemp)
                        newCmd1.Parameters.AddWithValue("@f3", User.Identity.Name)
                        newCmd1.ExecuteNonQuery()
                    End If
                End If
                cdTemp = -1
            Next
        oleDbConn.Close()
        cbl_Tags.DataBind()
        TagCloud.getTagCloud(SqlDataSource6, CloudTags, Page)
        cbl_Tags.ClearSelection()
        tb_Tags.Text = ""
        End If
    End Sub

    Protected Function SqlDataSource2_Selected(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource2.Selected
        If (e.AffectedRows < 1) Then
            Return False
        End If
        Return True
    End Function

    Protected Sub Button_Click(sender As Object, e As EventArgs)
        'My intention was to create one function which will add comments and replies.
        'However i couldn't create dynamically asp net buttons inside the literal which displays comments and replies
        'So this function is only used for adding comments.
        Dim btn As Button = DirectCast(sender, Button)
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "INSERT INTO [Comments] ([CommentsName], [Title], [Beaches_FK], [CommentsBody], [ReplyTo], [DateAdded]) VALUES (@f1, @f2, @f3, @f4, @f5, Now())"
        oleDbConn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_Title.Text)
        cmd.Parameters.AddWithValue("@f3", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f4", tb_Comment.Text)
        cmd.Parameters.AddWithValue("@f5", btn.CommandArgument)
        cmd.ExecuteNonQuery()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Comments.GetComments(Request.Params("ID"), CommentsLtr)
        getTotalComments()
        tb_Comment.Text = ""
        tb_Title.Text = ""
    End Sub
    Private Sub checkActivities()
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "SELECT * FROM Activities"
        oleDbConn.Close()

    End Sub

    Protected Sub btn_Favorites_Click(sender As Object, e As EventArgs) Handles btn_Favorites.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "UPDATE Favorite_Beach SET Beach_FK = @f1 WHERE Tourist = @f2"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd.Parameters.AddWithValue("f@2", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        'Eliminate cmd and oledbconn from memory
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()

        btn_Favorites.Text = "Your Favorite Beach"
        btn_Favorites.Enabled = False
    End Sub

    Protected Sub btn_AddGuide_Click(sender As Object, e As EventArgs) Handles btn_AddGuide.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "INSERT INTO GUIDES (Beach_FK, GuideCreator) VALUES (@f1, @f2)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd.Parameters.AddWithValue("f@2", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        'Eliminate cmd and oledbconn from memory
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()

        btn_AddGuide.Text = "Added To Guide"
        btn_AddGuide.Enabled = False
    End Sub

    Protected Function getViews(ByVal beachID As Integer) As String
        Dim result As String = ""
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "SELECT * FROM TotalViewsForAllBeaches WHERE ID = @f1"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        oleDbConn.Open()
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", beachID)
        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        dr.Read()
        result = dr("TotalViews")
        oleDbConn.Close()
        oleDbConn.Dispose()
        Return result
    End Function

    Protected Function getTotalComments() As String
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "SELECT * FROM TotalCommentsPerBeach WHERE ID = @f1"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        oleDbConn.Open()
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        dr.Read()
        Dim retValue As Integer = dr("TotalComments")
        If retValue <> 1 Then
            Return dr("TotalComments") & " Comments"
        Else
            Return dr("TotalComments") & " Comment"
        End If
        oleDbConn.Close()
        oleDbConn.Dispose()
    End Function

    Protected Sub btn_Rating_Click(sender As Object, e As EventArgs) Handles btn_Rating.Click
        'If user has selected a rating
        If Rating1.CurrentRating > 0 Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
            oleDbConn.Open()
            'Delete user's rating
            Dim delRatingSQL As String = "DELETE FROM BeachUserRating WHERE Beach_FK=@f1 AND Tourist=@f2"
            Dim delRatingCmd As New OleDbCommand(delRatingSQL, oleDbConn)
            delRatingCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            delRatingCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
            delRatingCmd.ExecuteNonQuery()
            'Add new rating
            Dim addRatingSql As String = "INSERT INTO BeachUserRating (Beach_FK, UserRating, Tourist) VALUES (@f1, @f2, @f3)"
            Dim addRatingCmd As New OleDbCommand(addRatingSql, oleDbConn)
            addRatingCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            addRatingCmd.Parameters.AddWithValue("@f2", Rating1.CurrentRating)
            addRatingCmd.Parameters.AddWithValue("@f3", User.Identity.Name)
            addRatingCmd.ExecuteNonQuery()
            oleDbConn.Close()
            oleDbConn.Dispose()
            addRatingCmd.Dispose()
            'Update repeaters which display the average rating and current rating
            Repeater5.DataBind()
            Repeater9.DataBind()

            btn_Rating.Visible = False
            btn_Change.Visible = True
            Repeater9.Visible = True
            Rating1.Visible = False
            'Update repeater's datasource which displays similar rated beaches
            SqlDataSource10.SelectParameters("@AverageRating").DefaultValue = getAverageRating.ToString
        End If
    End Sub

    Protected Sub SqlDataSource5_Selecting(sender As Object, e As SqlDataSourceSelectingEventArgs) Handles SqlDataSource5.Selecting
        e.Command.Parameters("Tourist").Value = User.Identity.Name
    End Sub

    Protected Sub btn_Change_Click(sender As Object, e As EventArgs) Handles btn_Change.Click
        btn_Rating.Visible = True
        btn_Change.Visible = False
        Repeater9.Visible = False
        Rating1.Visible = True
    End Sub

    Protected Sub btn_Likes_Click(sender As Object, e As EventArgs) Handles btn_Likes.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim addLikesql As String = "INSERT INTO Beach_Likes (Beach_FK, Tourist) VALUES (@f1, @f2)"
        Dim addLikecmd As OleDbCommand = New OleDbCommand(addLikesql, oleDbConn)
        addLikecmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        addLikecmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        addLikecmd.ExecuteNonQuery()
        oleDbConn.Close()
        oleDbConn.Dispose()
        addLikecmd.Dispose()
        'hide button
        rpt_Likes.DataBind()
        btn_Likes.Enabled = False
        btn_Likes.Text = "Liked"
        
    End Sub

    Protected Function getShareUrl() As String
        Return HttpContext.Current.Request.Url.AbsoluteUri
    End Function

    Protected Function getAverageRating() As Integer
        'Populate the repeater for displaying similar rated beaches
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim SqlStringRepeater As String = "SELECT AverageRating FROM BeachesWithAverageRating WHERE ID = @f1"
        Dim cmdRepeater As OleDbCommand = New OleDbCommand(SqlStringRepeater, oleDbConn)
        cmdRepeater.CommandType = CommandType.Text
        cmdRepeater.Parameters.AddWithValue("@f1", Request.Params("ID"))
        Dim beachRating As Integer
        Dim drRepeater As OleDbDataReader = cmdRepeater.ExecuteReader()
        drRepeater.Read()
        beachRating = drRepeater("AverageRating")
        oleDbConn.Close()
        oleDbConn.Dispose()
        Return beachRating
    End Function

    Protected Function getRegion() As String
        'Populate the repeater for displaying similar rated beaches
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim SqlStringRepeater As String = "SELECT Region FROM Beaches WHERE ID = @f1"
        Dim cmdRepeater As OleDbCommand = New OleDbCommand(SqlStringRepeater, oleDbConn)
        cmdRepeater.CommandType = CommandType.Text
        cmdRepeater.Parameters.AddWithValue("@f1", Request.Params("ID"))
        Dim beachRegion As String
        Dim drRepeater As OleDbDataReader = cmdRepeater.ExecuteReader()
        drRepeater.Read()
        beachRegion = drRepeater("Region")
        oleDbConn.Close()
        oleDbConn.Dispose()
        Return beachRegion
    End Function

    Protected Sub btn_AddActivity_Click(sender As Object, e As EventArgs) Handles btn_AddActivity.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        oleDbConn.Open()

        If tb_Activities.Text = "" Then

        Else
            Dim newActivities = tb_Activities.Text.Split(",")
            For Each newActivity In newActivities
                Dim newSqlString As String = "INSERT INTO Activities(ActivityName, Beaches_FK) VALUES (@f1, @f2)"
                Dim newCmd As OleDbCommand = New OleDbCommand(newSqlString, oleDbConn)
                newCmd.CommandType = CommandType.Text
                newCmd.Parameters.AddWithValue("@f1", newActivity)
                newCmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
                newCmd.ExecuteNonQuery()
                newCmd.Dispose()
            Next
        End If
        oleDbConn.Close()
        oleDbConn.Dispose()
        tb_Activities.Text = ""
        Repeater10.DataBind()

    End Sub

    Protected Function getTypeForPlace(ByVal restaurantInt As Boolean, ByVal coffeeInt As Boolean, ByVal barInt As Boolean) As String
        'Display if selected store is Restaurant, cafe, bar or all
        Dim result As String = "( "
        If (restaurantInt = True) Then
            result += "Restaurant "
        End If
        If (coffeeInt = True) Then
            result += "Cafe "
        End If
        If (barInt = True) Then
            result += "Bar "
        End If
        result += ")"

        Return result

    End Function

    Protected Function isRecommended(ByVal isRecommendedBool As Boolean) As String
        'Return if a hotel is recommended or not
        Dim result As String = "( "
        If isRecommendedBool = True Then
            result += "Recommended )"
        Else
            result += "Not recommended )"
        End If
        Return result

    End Function

    Protected Sub btn_AddFood_Click(sender As Object, e As EventArgs) Handles btn_AddFood.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        oleDbConn.Open()
        'Add a bar/cafe/restaurant to database.
        Dim restaurantInt As Integer = 0
        Dim coffeeInt As Integer = 0
        Dim barInt As Integer = 0

        Dim countFood As Integer = 0
        For Each cd As ListItem In cbl_Food.Items
            If cd.Selected = True Then
                Select Case (cd.Value)
                    Case "Restaurant"
                        restaurantInt = -1
                    Case "Coffee"
                        coffeeInt = -1
                    Case "Bar"
                        barInt = -1
                End Select
                countFood += 1
            End If
        Next
        If countFood = 0 Then
            lbl_ChooseType.Text = "Choose a type"
            oleDbConn.Close()
        Else
            Dim newSqlString As String = "INSERT INTO Cafes_Restaurant(CafeName, Restaurant, Coffee, Bar, Beaches_FK) VALUES (@f1, @f2, @f3, @f4, @f5)"
            Dim newCmd As OleDbCommand = New OleDbCommand(newSqlString, oleDbConn)
            newCmd.CommandType = CommandType.Text
            newCmd.Parameters.AddWithValue("@f1", tb_Food.Text)
            newCmd.Parameters.AddWithValue("@f2", restaurantInt)
            newCmd.Parameters.AddWithValue("@f3", coffeeInt)
            newCmd.Parameters.AddWithValue("@f4", barInt)
            newCmd.Parameters.AddWithValue("@f5", Request.Params("ID"))
            newCmd.ExecuteNonQuery()
            newCmd.Dispose()
            tb_Food.Text = ""
            lbl_ChooseType.Text = ""
            For Each cd As ListItem In cbl_Food.Items
                cd.Selected = False
            Next
            oleDbConn.Close()
            oleDbConn.Dispose()
            Repeater11.DataBind()
        End If
    End Sub

    Protected Sub btn_Hotel_Click(sender As Object, e As EventArgs) Handles btn_Hotel.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        oleDbConn.Open()
        'Add a hotel to database
        Dim SqlString As String = "INSERT INTO Hotels(HotelName, isRecommended, Beach_FK, Facilities, Location) VALUES (@f1, @f2, @f3, @f4, @f5)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_HotelName.Text)
        cmd.Parameters.AddWithValue("@f2", rbl_Hotel.SelectedValue)
        cmd.Parameters.AddWithValue("@f3", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f4", tb_Facilities.Text)
        cmd.Parameters.AddWithValue("@f5", tb_HotelLocation.Text)
        cmd.ExecuteNonQuery()

        tb_HotelName.Text = ""
        tb_Facilities.Text = ""
        tb_HotelLocation.Text = ""
        oleDbConn.Close()
        oleDbConn.Dispose()
        Repeater12.DataBind()
    End Sub
End Class